<?php
/**
 * 相关Request内容  
 *
 * @filesource _request.class.php
 * @package peck
 * @subpackage 
 * @version $id: 0.1, utf8, Tue Nov 30 14:23:54 CST 2010$
 * @author LD King <kldscs[at]gmail.com>
 * @copyright Copyleft (D.) 2007 - 2010 LD pr.
 * @link 
 * @example 
 *   
 */

class _request {

    /**
     * 返回请求的信息 $_GET, $_POST, $_REQUEST ?
     *
     * @param string $param 
     * @param mixed $default 默认值
     *
     * @return mixed
     */
    public static function req($param, $default=null){
        if(isset($_GET[$param])){
            return $_GET[$param];
    }elseif(isset($_POST[$param])){
        return $_POST[$param];
    }else
        return $default;
    }

    /**
     * 返回请求的信息 $_GET, $_POST, $_REQUEST ?
     *   判断时使用 empty
     * @param string $param 
     * @param mixed $default 默认值
     *
     * @return mixed
     */
    public static function req2($param, $default=null){
        if(!empty($_GET[$param])){
            return $_GET[$param];
    }elseif(!empty($_POST[$param])){
        return $_POST[$param];
    }else
        return $default;
    }
    
    /**
     * 获得当前url
     *
     * @return string
     */
    public static function current_url(){
        $url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ? 'https://' : 'http://';
        $url .= $_SERVER["SERVER_NAME"].(  (80==$_SERVER["SERVER_PORT"]) ? '' : (':'.$_SERVER["SERVER_PORT"]) )
            .self::request_uri();
        return $url;
    }


    /**
     * 获取 _SERVER['REQUEST_URI'] 值的通用解决方案
     *
     */
    public static function request_uri(){
        if (isset($_SERVER['REQUEST_URI'])){
            $uri = $_SERVER['REQUEST_URI'];
        }else{

            if (isset($_SERVER['argv'])){
                $uri = $_SERVER['PHP_SELF'] .'?'. $_SERVER['argv'][0];
            }else{
                $uri = $_SERVER['PHP_SELF'] . 
                    (empty($_SERVER['QUERY_STRING']) ? '' : ('?'.$_SERVER['QUERY_STRING']));
            }
        }
        return $uri;
    }


    /**
     * 获得客户 IP地址, 如果没有 返回 false
     * 
     * @return mixed 
     */
    public static function client_ip(){
        if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
            $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
        else if (isset($_SERVER["HTTP_CLIENT_IP"]))
            $ip = $_SERVER["HTTP_CLIENT_IP"];
        else if (isset($_SERVER["REMOTE_ADDR"]))
            $ip = $_SERVER["REMOTE_ADDR"];
        else if (getenv("HTTP_X_FORWARDED_FOR"))
            $ip = getenv("HTTP_X_FORWARDED_FOR");
        else if (getenv("HTTP_CLIENT_IP"))
            $ip = getenv("HTTP_CLIENT_IP");
        else if (getenv("REMOTE_ADDR"))
            $ip = getenv("REMOTE_ADDR");
        else
            $ip = false;
        return $ip;
    }//END func ip

    /**
     * 获取当前用户来源
     *
     * @return string
     */
    public static function referer(){
        return isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null;
    } 

}//END class
